package leo.mystudy.leetcode.nodelist;

import leo.mystudy.leetcode.ListNode;

import java.util.HashSet;
import java.util.Set;

/**
 * @author leo
 * @version 1.0.0
 * @description
 * @create 2022/3/15 20:39
 */
public class HasCycle {
    public boolean hasCycle(ListNode head) {
        Set<ListNode> set = new HashSet<>();
        ListNode cur = head;
        while (cur != null) {
            set.add(cur);
            if (set.contains(cur.next)) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public static void main(String[] args) {
        ListNode node = new ListNode(2);
        node.next = new ListNode(3);
        node.next.next = new ListNode(4);
        node.next.next.next = node.next;
        System.out.println(new HasCycle().hasCycle(node));
    }
}
